import {_decorator, Component, Label, Node, profiler, Sprite, SpriteFrame, Vec3} from 'cc';
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {UIManager} from "db://assets/scripts/common/UIManager";
import {StorageManager} from "db://assets/scripts/common/StorageManager";
import {StorageKey} from "db://assets/scripts/common/StorageKey";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";

const {ccclass, property} = _decorator;

/**
 * 设置界面
 */
@ccclass('SettingController')
export class SettingController extends Component {
    /**
     * 选中时的图片
     */
    @property(SpriteFrame)
    public selectSF: SpriteFrame;
    /**
     * 未选中时的图片
     */
    @property(SpriteFrame)
    public unSelectSF: SpriteFrame;
    /**
     * 振动
     */
    @property(Node)
    public vibrationButton: Node;
    /**
     * 音乐
     */
    @property(Node)
    public musicButton: Node;
    /**
     * 调试
     */
    @property(Node)
    public debugButton: Node;

    /**
     * 是否开启音乐
     */
    private isMusicOpen: boolean = false;
    /**
     * 是否开启振动
     */
    private isVibrationOpen: boolean = false;
    /**
     * 是否开启调试模式
     */
    private isDebugOpen: boolean = false;
    /**
     * 当前选中点的位置
     */
    private currentDotPosition: Vec3 = new Vec3();

    /**
     * 显示设置界面
     */
    public show() {
        this.isMusicOpen = AudioManager.getInstance().getMusicEnable();
        this.changeState(this.musicButton, this.isMusicOpen);

        this.isVibrationOpen = Boolean(StorageManager.getInstance().getData(StorageKey.VIBRATION));
        this.changeState(this.vibrationButton, this.isVibrationOpen);

        this.isDebugOpen = Boolean(StorageManager.getInstance().getData(StorageKey.DEBUG));
        this.changeState(this.debugButton, this.isDebugOpen);
    }

    /**
     * 改变状态
     *
     * @param target 目标节点
     * @param isOpen 是否开启
     */
    private changeState(target: Node, isOpen: boolean) {
        let iconSprite = target.getComponent(Sprite);
        let dotNode = target.getChildByName("dot");
        let lbTxt = dotNode.getChildByName("txt")?.getComponent(Label);
        let ndDotPos = dotNode.position;

        if (isOpen) {
            iconSprite.spriteFrame = this.selectSF;
            this.currentDotPosition.set(24, ndDotPos.y, ndDotPos.z);
            dotNode.setPosition(this.currentDotPosition);
            lbTxt.string = "开";
        } else {
            iconSprite.spriteFrame = this.unSelectSF;
            this.currentDotPosition.set(-24, ndDotPos.y, ndDotPos.z);
            dotNode.setPosition(this.currentDotPosition);
            lbTxt.string = "关";
        }
    }

    public onVibration() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        this.isVibrationOpen = !this.isVibrationOpen;
        this.changeState(this.vibrationButton, this.isVibrationOpen);
        StorageManager.getInstance().setData(StorageKey.VIBRATION, String(this.isVibrationOpen));
    }

    public onMusic() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        this.isMusicOpen = !this.isMusicOpen;
        this.changeState(this.musicButton, this.isMusicOpen);

        if (this.isMusicOpen) {
            AudioManager.getInstance().openMusic();
            AudioManager.getInstance().openSound();
        } else {
            AudioManager.getInstance().closeMusic();
            AudioManager.getInstance().closeSound();
        }
    }

    /**
     * Debug
     */
    public onDebug() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        this.isDebugOpen = !this.isDebugOpen;
        this.changeState(this.debugButton, this.isDebugOpen);
        StorageManager.getInstance().setData(StorageKey.DEBUG, String(this.isDebugOpen));

        // showStats：显示性能信息
        // hideStats：隐藏性能信息
        this.isDebugOpen === true ? profiler.showStats() : profiler.hideStats();
    }

    /**
     * 关闭事件
     */
    public onClose() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);
        // 隐藏设置界面
        UIManager.getInstance().hideDialog("setting/settingPanel");
    }
}